package cm.iccsoft.android.telecomcost.activities;

import java.util.ArrayList;
import com.google.gson.Gson;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.Utility;
import cm.iccsoft.android.telecomcost.adapter.CountryAdapter;
import cm.iccsoft.android.telecomcost.entities.Country;
import cm.iccsoft.android.telecomcost.ui.CountryDialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

public class CountryListActivity extends TelecomcostActivity {

	private ListView list;
	private DatabaseManager db;
	private ArrayList<Country> countries;
	public static final int DIALOG_NEW_COUNTRY = 1;
	public static final int DIALOG_EDIT_COUNTRY = 2;

	@Override
	public Dialog onCreateDialog(int id) {
		Log.i("CountryListActivity", "Creating dialog ohne bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_NEW_COUNTRY:
			dialog = new CountryDialog(CountryListActivity.this, null);
			break;
		default:
			dialog = null;
		}
		return dialog;
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		Log.i("CountryListActivity", "Creating dialog with bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_EDIT_COUNTRY:
			Gson gson = TelecomCost.getGson();
			Country country = gson.fromJson(args.getString(Country.PROP_TYPE),
					Country.class);
			if (country != null) {
				Log.i("CountryListActivity",
						"launching dialog to edit country " + country.getName());
				dialog = new CountryDialog(CountryListActivity.this,
						country);
			} else {
				Log.i("CountryListActivity","country is null,cannot create an edit dialog");
				return null;
			}
			break;
		default:
			dialog = null;
		}

		if (dialog == null) {
			return onCreateDialog(id);
		}

		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		Log.i("CountryListActivity", "onPrepareDialog with bundle : ");			

		super.onPrepareDialog(id, dialog, args);
		if(args != null)
		{
			if (args.containsKey(Country.PROP_TYPE)) {
				Gson gson = TelecomCost.getGson();
				Country country = gson.fromJson(args.getString(Country.PROP_TYPE),
						Country.class);
				Log.i("CountryListActivity", "preparing dialog to edit country "
						+ country.getName());
				((CountryDialog) dialog).setCountry(country);
			}
		}
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		Log.i("CountryListActivity", "onPrepareDialog ohne bundle : ");
		super.onPrepareDialog(id, dialog);		
		Utility.setDialogLayout(dialog);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("CountryListActivity", "creating options menu for zones : ");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_zones, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_item_new_zone:
			showDialog(DIALOG_NEW_COUNTRY);
			return true;
		case R.id.menu_item_delete_zones:
			deleteCountries();
			bindCountries();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("CountryListActivity", "Creating the activity " + this.getTitle());

		super.onCreate(savedInstanceState);
		db = TelecomCost.getDatabaseManager();

		setContentView(R.layout.country_list);

		list = (ListView) findViewById(R.id.listViewCountries);

		bindCountries();
	}

	protected void deleteCountries() {
		Log.i("CountryListActivity", "deleting selected countries : ");
		int i = 0;		

		while (i < countries.size()) {
			Country country = countries.get(i);
			if (country.isSelected()) {
				db.deleCountry(country);										
			}
			i++;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		Log.i("CountryListActivity",
				"Destroying the activity " + this.getTitle());
		db = null;
		list = null;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		Log.i("CountryListActivity", "Pausing the activity " + this.getTitle());
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		Log.i("CountryListActivity",
				"Postcreating the activity " + this.getTitle());
	}

	@Override
	protected void onPostResume() {
		// TODO Auto-generated method stub
		super.onPostResume();
		Log.i("CountryListActivity",
				"PostResuming the activity " + this.getTitle());
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.i("CountryListActivity",
				"Restarting the activity " + this.getTitle());
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		Log.i("CountryListActivity", "onRestoreInstanceState the activity "
				+ this.getTitle());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("CountryListActivity", "Resuming the activity " + this.getTitle());
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		Log.i("CountryListActivity",
				"onSaveInstanceState the activity " + this.getTitle());
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("CountryListActivity", "Starting the activity " + this.getTitle());
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i("CountryListActivity", "Stopping the activity " + this.getTitle());
	}

	public void bindCountries() {
		Log.i("CountryListActivity", "binding countries");
		countries = db.getCountries();

		if (list != null) {
			list.setAdapter(new CountryAdapter(CountryListActivity.this,
					R.layout.item_row, (ArrayList<Country>) countries, this));
		} else {
			Log.e("ListView is null", null);
		}
	}

	@Override
	public String toString() {
		return "CountryListActivity";
	}

	public Country getCountry(String name) {
		String _name;
		int i = 0;
		while (i < countries.size()) {
			_name = countries.get(i).getName();
			if (_name.equals(name)) {
				Log.i("CountryListActivity",
						"this country already exists...we update it");
				return countries.get(i);
			}
			i++;
		}

		return null;
	}
}
